// export const startScan = () => {
//   let type = false
//   uni.getSetting({
//     success: (res) => {
//       // 判断是否已经授予相机权限
//       if (res.authSetting['scope.camera']) {
//         // 已经授予相机权限，调用扫码功能
//         type = true
//       } else {
//         // 未授予相机权限，请求授权
//         uni.authorize({
//           scope: 'scope.camera',
//           success: () => {
//             // 用户同意授权，调用扫码功能
//             type = true
//           },
//           fail: () => {
//             // 用户拒绝授权，提示用户去设置中开启权限
//             uni.showModal({
//               title: '提示',
//               content: '请在设置中开启相机权限，以使用扫码功能',
//               confirmText: '去设置',
//               success: (modalRes) => {
//                 if (modalRes.confirm) {
//                   // 打开设置页面
//                   uni.openSetting();
//                 }
//               }
//             });
//           }
//         });
//       }
//     }
//   });
//   return type
// }


// export const scanCode = () => {
//   uni.scanCode({
//     success: (res) => {
//       // 扫码成功，处理扫码结果
//       console.log('扫码结果：', res.result);
//       uni.showToast({
//         title: '扫码成功',
//         icon: 'success'
//       });
//     },
//     fail: (err) => {
//       // 扫码失败，处理错误信息
//       console.error('扫码失败：', err);
//       uni.showToast({
//         title: '扫码失败',
//         icon: 'none'
//       });
//     }
//   });
// }


// utils/dateUtils.js
export function timestampToDate(timestamp) {
  const date = new Date(timestamp);
  const year = date.getFullYear();
  const month = String(date.getMonth() + 1).padStart(2, '0');
  const day = String(date.getDate()).padStart(2, '0');
  return `${ year }-${ month }-${ day }`;
}





export const calculateAverage = (arr) => {
  if (!Array.isArray(arr) || arr.length === 0) {
    return 0;
  }
  const sum = arr.reduce((acc, val) => acc + val, 0);
  return sum / arr.length;
}


// 电量
export function formatElectricity(electricity) {
  if (isNaN(electricity)) {
    return electricity;
  }
  if (electricity < 1000) {
    return `${ electricity } kWh`;
  } else if (electricity < 1000 * 1000) {
    const mw = (electricity / 1000).toFixed(2);
    return `${ mw } MWh`;
  } else {
    const gw = (electricity / (1000 * 1000)).toFixed(2);
    return `${ gw } GWh`;
  }
}





export function formatElectricityOfUnit(electricity, unit) {
  if (isNaN(electricity)) {
    return electricity;
  }
  if (unit == 'kWh') {
    return electricity
  } else if (unit == 'MWh') {
    return (electricity / 1000).toFixed(2)
  } else if (unit == 'GWh') {
    return (electricity / (1000 * 1000)).toFixed(2)
  }
}

export const chargeUnit = (params, index) => {
  if (formatElectricity(params)) {
    let arr = formatElectricity(params).split(" ");
    return arr[index];
  }
};


// 功率
export function formatPower(electricity) {
  if (isNaN(electricity)) {
    return electricity;
  }
  if (electricity < 1000) {
    return `${ electricity } kW`;
  } else if (electricity < 1000 * 1000) {
    const mw = (electricity / 1000).toFixed(2);
    return `${ mw } MW`;
  } else {
    const gw = (electricity / (1000 * 1000)).toFixed(2);
    return `${ gw } GW`;
  }
}
export const powerUnit = (params, index) => {
  if (formatPower(params)) {
    let arr = formatPower(params).split(" ");
    return arr[index];
  }
};
export function formatPowerUnitOfUnit(electricity, unit) {
  if (isNaN(electricity)) {
    return electricity;
  }
  if (unit == 'kW') {
    return electricity
  } else if (unit == 'MW') {
    return (electricity / 1000).toFixed(2)
  } else if (unit == 'GW') {
    return (electricity / (1000 * 1000)).toFixed(2)
  }
}


export function formatMoney(electricity) {
  const result = { templete: '{value}', value: 0 };
  if (isNaN(electricity)) {
    result.value = electricity;
  }
  if (electricity < 10000) {
    result.templete = "unit.yuan";
    result.value = electricity;
  } else if (electricity < 10000 * 10000) {
    const mw = (electricity / 10000).toFixed(2);
    result.templete = "unit.wanyuan";
    result.value = mw;
  } else {
    const gw = (electricity / (10000 * 10000)).toFixed(2);
    result.templete = "unit.qianwanyun";
    result.value = gw;
  }
  return result;
}



export function formatMoneyOfUnit(electricity, unit) {
  if (unit == 'unit.yuan') {
    return electricity
  } else if (unit == 'unit.wanyuan') {
    return (electricity / 10000).toFixed(2)
  } else if (unit == 'unit.qianwanyun') {
    return (electricity / (10000 * 10000)).toFixed(2)
  }
}

export const moneyUnit = (params, index) => {
  if (formatMoney(params)) {
    return formatMoney(params)
  }
};







export const formatCurrent = (number) => {
  let current = Number(number)
  const result = { templete: '{value}', value: 0 };
  if (isNaN(current)) {
    result.value = current;
  }
  // 电流值小于1000A，保持为安培（A）
  if (current < 10000) {
    result.templete = "A";
    result.value = current.toFixed(2);
  }
  if (current >= 1000) {
    // 如果电流值大于等于1000A，转换为千安（kA）
    const gw = (current / 1000).toFixed(2);
    result.templete = " kA";
    result.value = gw;
  }
  return result
}



export function formatCurrentUnitOfUnit(electricity, unit) {
  if (isNaN(electricity)) {
    return electricity;
  }
  if (unit == 'A') {
    return electricity
  } else if (unit == 'kA') {
    return (current / 1000).toFixed(2);
  }
}